package mate.business
{
	import com.asfusion.mate.core.GlobalDispatcher;
	
	import mate.events.LoginEvent;
	import mate.events.UpdateRegistrationEvent;
	
	import mx.utils.object_proxy;
	
	import spark.components.NavigatorContent;
	
	public class UpdateRegistrationManager
	{
		import classespinkpony.*;
		
		import mx.collections.ArrayCollection;
		import mx.controls.Alert;
		import mx.rpc.Fault;
		
		//[Bindable]
		//public var selectedView: int = 0;
		
		private var _practice:Practice;
		private var _address:Address;
		private var _user:User;
		
		private var _locations:ArrayCollection;
		private var _countries:ArrayCollection;
		private var _districts:ArrayCollection;
		private var _practices:ArrayCollection;
		
		private var _newPractices:ArrayCollection;
		
		private var insertedNewPractices:ArrayCollection;
		
		private var disp:GlobalDispatcher;
		
		public function UpdateRegistrationManager(){
			disp = new GlobalDispatcher();
		}
		
		
		[Bindable]
		public function get practices():ArrayCollection
		{
			return _practices;
		}

		public function set practices(value:ArrayCollection):void
		{
			_practices = value;
		}

		public function get practice():Practice
		{
			return _practice;
		}

		[Bindable]
		public function set practice(value:Practice):void
		{
			_practice = value;
		}
		
		public function storePractice(practice:Object):void
		{
			if(practice != null){
				for each (var pract:Practice in practice)
				{
					this.practice = pract;
				}
			}
		}

		[Bindable]
		public function get locations():ArrayCollection
		{
			return _locations;
		}
		
		public function set locations(value:ArrayCollection):void
		{
			_locations = value;
		
		}
		
		public function storeLocations(locations:Object):void
		{
			var temp: ArrayCollection = new ArrayCollection();
			
			if(locations != null){
				for each (var location:Location in locations)
				{
					temp.addItem(location);
				}
			}
			this.locations = temp;
		}

		[Bindable]
		public function get countries():ArrayCollection
		{
			return _countries;
		}

		public function set countries(value:ArrayCollection):void
		{
			_countries = value;
		}
		
		public function storeCountries(countries:Object):void
		{
			var temp : ArrayCollection = new ArrayCollection();
			
			if(countries != null){
				for each (var country:Country in countries)
				{
					temp.addItem(country);
				}
			}
			this.countries = temp;
		}

		[Bindable]
		public function get districts():ArrayCollection
		{
			return _districts;
		}

		public function set districts(value:ArrayCollection):void
		{
			_districts = value;
		}
		
		public function storeDistricts(districts:Object):void
		{
			var temp : ArrayCollection = new ArrayCollection();
			
			if(districts != null){
				for each (var district:District in districts)
				{
					temp.addItem(district);
				}
			}
			this.districts = temp;
		}		


		public function get address():Address
		{
			return _address;
		}
		[Bindable]
		public function set address(value:Address):void
		{
			_address = value;
		}
		
		public function storeAddress(address:Object):void
		{
			if(address != null){
				for each (var a:Address in address)
				{
					this.address = a;	
				}

			}
			
		}
		
		public function saveNewPractices(practices:ArrayCollection): void{
			//Loopen over alle practices en voor elk een event aanroepen
			this.newPractices = practices;
			this.insertedNewPractices = new ArrayCollection();
			for(var i : int = 0; i < newPractices.length;i++){
				var prac : Practice = newPractices.getItemAt(i) as Practice;
				var savePracticeAddressEvent:UpdateRegistrationEvent = new UpdateRegistrationEvent(UpdateRegistrationEvent.SAVEPRACTICEADDRESS);
				savePracticeAddressEvent.address = prac.address;
				savePracticeAddressEvent.searchNumber = i;
				
				disp.dispatchEvent(savePracticeAddressEvent);
			}
				
		}
		
		public function saveNewPractice(practiceAddressId:Object, practiceIndex: int):void{
			var prac: Practice = newPractices.getItemAt(practiceIndex) as Practice;
			prac.address_id = practiceAddressId.toString();
			var savePracticeEvent: UpdateRegistrationEvent = new UpdateRegistrationEvent(UpdateRegistrationEvent.SAVENEWPRACTICE);
			savePracticeEvent.practice = prac;
			
			disp.dispatchEvent(savePracticeEvent);
		}
		
		public function insertingPracticeComplete(practice:Practice): void{
			
			insertedNewPractices.addItem(practice);
			if(insertedNewPractices.length == newPractices.length){
				
				var showControlpanel:LoginEvent = new LoginEvent(LoginEvent.LOADCONTROLPANEL,user);
				showControlpanel.user = user;
				disp.dispatchEvent(showControlpanel);
				
				
			}
		}
		

		[Bindable]
		public function get newPractices():ArrayCollection
		{
			return _newPractices;
		}

		public function set newPractices(value:ArrayCollection):void
		{
			_newPractices = value;
		}

		[Bindable]
		public function get user():User
		{
			return _user;
		}

		public function set user(value:User):void
		{
			_user = value;
		}


	}

	}